package com.dehui.traders.ui.market.broader;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.bean.IndexInfo;
import com.dehui.traders.config.Config;
import com.dehui.traders.databinding.FragmentTheBroaderMarketBinding;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.widgets.MyHScrollView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Created by hqq on 18.4.2.
 */

public class TheBroaderMarketFragment extends BaseFragment<FragmentTheBroaderMarketBinding, TheBroaderMarketViewModel> implements TableAdapter.OnTableItemClick {

    public HorizontalScrollView mTouchView;
    private View view;
    private boolean isInit = false;
    private List<IndexInfo> indexList = new ArrayList<>();
    private TheBroaderAdapter adapter;
    private SmartRefreshLayout refreshLayout;

    @Override
    public int initContentView() {
        return R.layout.fragment_the_broader_market;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public TheBroaderMarketViewModel initViewModel() {
        return new TheBroaderMarketViewModel(getContext(),this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            view = super.onCreateView(inflater, container, savedInstanceState);
            initViews();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
        return view;
    }


    private void initViews() {
        refreshLayout = view.findViewById(R.id.sr_refresh);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        adapter = new TheBroaderAdapter(getContext(), indexList, this);
        binding.tabView.setFixes(Config.indexTitle)
                .setOthers(Config.indexOtherTitle)
                .setFragment(this)
                .setOnItemClick(this)
                .setAdapter(adapter).build();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                viewModel.getIndex();
            }
        });
    }

    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        List<MyHScrollView> mHScrollViews = binding.tabView.getScrollViews();
        for(MyHScrollView scrollView : mHScrollViews) {
            if(mTouchView != scrollView)
                scrollView.smoothScrollTo(l, t);
        }
    }

    public void completeRefresh() {
        refreshLayout.finishRefresh();
    }

    public void updataList(List<IndexInfo> list) {
        indexList.clear();
        indexList.addAll(list);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onItemClick(int position) {
        IndexInfo indexInfo = indexList.get(position);
        StocksDetailActivity.startType(getActivity(),"大盘",indexInfo.name,indexInfo.code,String.valueOf(indexInfo.preclose),indexInfo.market,1);
    }

}
